﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CNRegex.Biz.BizAccess;
using CNRegex.Web.Base;
using CNRegex.Entity;
using CNRegex.Web.Utility;
using Project.Common;
using CNRegex.Enum;
using CNRegex.Biz;

namespace CNRegex.Web.Controllers
{
	public class RegexController : BaseController
	{
		private BizRegexCategory bizRegexCategory;
		private BizRegexExpression bizRegexExpression;
		private BizRegexFavourite bizRegexFavorite;
		public RegexController()
		{

		}
		//
		// GET: /Regex/
		public ActionResult Index()
		{
			
			return View();
		}

		public ActionResult Create()
		{
			ViewBag.Title = "创建正则表达式";
			SelectList selectList = new SelectList(bizRegexCategory.List(x => x.OrderIndex, Enum.EnumOrder.ASC),"ID", "Name");
			ViewBag.CategoryID = WebTools.InsertOption(selectList, "", "",false, Enum.EnumLoaction.First);
			return View();
		}

		[HttpPost]
		[ValidateInput(false)]
		public ActionResult Create(RegexExpression model, string[] nonMatch,string[] match )
		{
			model.CreateDate = DateTime.Now;
			model.ClickCount = 0;
			model.UserID = FrontContext.CurrentRegexUser.ID;
			model.NonMatches = StringUtility.ArrayJoin(nonMatch.Where(x=>!string.IsNullOrEmpty(x)).ToArray(),"|||");
			model.Matches = StringUtility.ArrayJoin(match.Where(x =>! string.IsNullOrEmpty(x)).ToArray(), "|||");
			bizRegexExpression.Save(model);
			return Redirect(string.Format("/{0}",FrontContext.CurrentRegexUser.Name));
		}

		public ActionResult Edit(Guid id)
		{
			ViewBag.Title = "编辑正则表达式";
			RegexExpression model = bizRegexExpression.GetByID(id);
			SelectList selectList = new SelectList(bizRegexCategory.List(x => x.OrderIndex, Enum.EnumOrder.ASC), "ID", "Name",model.CategoryID);
			ViewBag.CategoryID = WebTools.InsertOption(selectList, "", "", false, Enum.EnumLoaction.First);
			return View("Create", model);
		}

		[HttpPost]
		public ActionResult Preserver(Guid id)
		{
			if (FrontContext.CurrentRegexUser == null)
				return Content("login");
			RegexFavourite model = new RegexFavourite()
			{
				FavoriteID = id,
				FavouriteDate = DateTime.Now,
				FavouriteGroupID = null,
				Remark = string.Empty,
				UserID = FrontContext.CurrentRegexUser.ID,
				Type= (int)EnumFavoriteType.Expression
			};
			bizRegexFavorite.Save(model);
			return Content("");
		}

		[HttpPost]
		public ActionResult Recommend(Guid id)
		{
			RegexExpression model = bizRegexExpression.GetByID(id);
			model.Recommend = model.Recommend == null ? 1 : model.Recommend + 1;
			bizRegexExpression.Save(model);
			return Content("");
		}


		[HttpPost]
		[ValidateInput(false)]
		public ActionResult Edit(RegexExpression model, string[] nonMatch, string[] match,string c,int? p)
		{
			RegexExpression original = bizRegexExpression.GetByID(model.ID);
			original.NonMatches = StringUtility.ArrayJoin(nonMatch.Where(x => !string.IsNullOrEmpty(x)).ToArray(), "|||");
			original.Matches = StringUtility.ArrayJoin(match.Where(x => !string.IsNullOrEmpty(x)).ToArray(), "|||");
			original.Expression = model.Expression;
			original.Description = model.Description;
			original.Title = model.Title;
			bizRegexExpression.SaveOrUpdate(original);
			return Redirect(string.Format("/{0}?{1}p={2}&t={3}", FrontContext.CurrentRegexUser.Name, string.IsNullOrEmpty(c)?"":string.Format("c={0}&",c), p, (int)EnumExpressionType.myExpression));
		}
	}
}
